/**
 * 
 */
package com.zj.helloview.treeviewer.commands;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

import com.zj.helloview.TableView;
import com.zj.helloview.model.ModelProvider;
import com.zj.helloview.treeviewer.dialogs.AddPersonDialog;

/**
 * @author Jeff.Zheng
 * 
 */
public class AddPerson extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		ModelProvider persons = ModelProvider.getInstance();
		AddPersonDialog dialog = new AddPersonDialog(window.getShell());
		dialog.open();
		if (dialog.getPerson() != null) {
			persons.getPersons().add(dialog.getPerson());
			// Updating the display in the view
			IWorkbenchPage page = window.getActivePage();
			TableView view = (TableView) page.findView(TableView.ID);
			view.getViewer().refresh();
		}
		return null;
	}

}
